package com.yjhealth.libs.core.view.wordsnavigation;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.yjhealth.libs.core.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 字母导航
 */
public class WordsNavigationVertical extends View {
    /*Default*/
    public static final String WORDSALL[] = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    /*Util*/
    private OnWordsChangeListener listener;
    //字母背景画笔
    private Paint wordsPaint;
    //字母背景画笔
    private Paint bgPaint;
    /*Flag*/
    private int textNoSelectColor;
    private int textSelectColor;
    private int textBgColor;
    private List<String> words;

    private float singleHeight;
    //向下偏移
    private float exY;
    //手指按下的字母索引
    private int touchIndex = 0;
    /*View*/

    public WordsNavigationVertical(Context context) {
        this(context, null);
    }

    public WordsNavigationVertical(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WordsNavigationVertical(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.yjhealthCoreWordsNavigation);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.yjhealthCoreWordsNavigation_text_noselect_color) {
                textNoSelectColor = typedArray.getColor(attr, Color.BLACK);
            } else if (attr == R.styleable.yjhealthCoreWordsNavigation_text_select_color) {
                textSelectColor = typedArray.getColor(attr, Color.WHITE);
            } else if (attr == R.styleable.yjhealthCoreWordsNavigation_text_bg_color) {
                textBgColor = typedArray.getColor(attr, Color.GRAY);
            }
        }
        typedArray.recycle();
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();

        singleHeight = height / words.size();
        if (singleHeight > 40) {
            singleHeight = 40;
        }

        //向下偏移
        exY = (height - singleHeight * words.size()) / 2;

        //float wordSize = singleHeight < width ? singleHeight * 8 / 10 : width * 8 / 10;
        float wordSize = getResources().getDimension(R.dimen.sp_12);
        wordsPaint.setTextSize(wordSize);

        for (int i = 0; i < words.size(); i++) {
            float wordWidth = wordsPaint.measureText(words.get(i));
            float wordX = width / 2 - wordWidth / 2;
            float wordY = singleHeight * i + singleHeight + exY;
            if (touchIndex == i) {
                canvas.drawCircle(wordX + wordWidth / 2, wordY - (wordSize - 4) / 2,
                        (wordSize + 10) / 2, bgPaint);
                wordsPaint.setColor(textSelectColor);
            } else {
                wordsPaint.setColor(textNoSelectColor);
            }
            canvas.drawText(words.get(i), wordX, wordY, wordsPaint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = touchIndex;
        final int c = (int) ((y - exY) / singleHeight);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < words.size()) {
                        listener.wordsChange(words.get(c));
                        touchIndex = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < words.size()) {
                        listener.wordsChange(words.get(c));
                        touchIndex = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    /**
     * 初始化画笔
     */
    private void init() {
        wordsPaint = new Paint();
        wordsPaint.setFakeBoldText(true);
        wordsPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(textBgColor);

        words = new ArrayList<>();
        for (int i = 0; i < WORDSALL.length; i++) {
            words.add(WORDSALL[i]);
        }
    }

    /**
     * 设置words,list可以重复，无视顺序
     *
     * @param list
     */
    public void setwords(List<String> list) {
        words.clear();
        words.add("#");
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < WORDSALL.length; i++) {
                if (WORDSALL[i].equals("#")) {
                    continue;
                }
                for (String word : list) {
                    if (WORDSALL[i].equalsIgnoreCase(word)) {
                        words.add(WORDSALL[i]);
                    }
                }
            }
        }

        invalidate();

    }

    /*设置当前按下的是那个字母*/
    public void setTouchIndex(String word) {
        if (TextUtils.isEmpty(word)) {
            return;
        }
        for (int i = 0; i < words.size(); i++) {
            if (words.get(i).equalsIgnoreCase(word)) {
                if (touchIndex == i) break;
                touchIndex = i;
                invalidate();
                return;
            }
        }
    }

    /*手指按下了哪个字母的回调接口*/
    public interface OnWordsChangeListener {
        void wordsChange(String words);
    }

    /*设置手指按下字母改变监听*/
    public void setOnWordsChangeListener(OnWordsChangeListener listener) {
        this.listener = listener;
    }
}

